'use strict';
angular.module('app')
    .directive('adminiDivision', function () {
        return {
            restrict: 'E',
            scope: {
                showAdminiDivision: '=showDivision',
                selectedPlace: '=',
                setPlace: '='
            },
            transclude: true,
            templateUrl: "./src/app/launchplan/adminiDivision.html?v="+v,
            controller: ['$rootScope', '$scope', '$http', '$state','$timeout', function ($rootScope, $scope, $http, $state,$timeout) {
                $scope.targetPlace = '' //搜索框清空
                $scope.dropShow = false; //搜索下拉框默认隐藏
                if ($scope.selectedPlace) {
                    var origionPlace = JSON.parse(JSON.stringify($scope.selectedPlace))
                }
                //一级区域获取
                $.ajax({
                    url: $rootScope.app.project_name + '/zzyLaunchPlan/getChCity',
                    data: {
                        cityId:'10000'
                    }
                }).then(function (res) {
                    if (res.httpCode == 200) {
                        $scope.$apply(function () {
                            console.log(res)
                            $scope.provinces = res.data.filter(function (v) {
                                return v.region_type == '1'
                            });
                            checkSelect()
                        })
                    } else {
                        $rootScope.alertModal({"content":"系统异常"});
                    }
                })
                //点击一级获取二级区域
                $scope.showCity = function (province) {
                    if (!province.cityList) {
                        $.ajax({
                            url: $rootScope.app.project_name + '/zzyLaunchPlan/getChCity',
                            data: {
                                cityId:province.id
                            }
                        }).then(function (res) {
                            console.log(res);
                            if (res.httpCode == 200) {
                                $scope.$apply(function () {
                                    province.cityList = res.data
                                    checkSelect()
                                })
                            } else {
                                $rootScope.alertModal({"content":"系统异常"});
                            }
                        })
                    }
                    province.open = province.open ? !province.open : true
                }

                //点击二级获取三级区域
                $scope.showCounty = function (city) {
                    if (!city.countyList) {
                        $.ajax({
                            url: $rootScope.app.project_name + '/zzyLaunchPlan/getChCity',
                            data: {
                                cityId:city.id
                            }
                        }).then(function (res) {
                            console.log(res);
                            if (res.httpCode == 200) {
                                $scope.$apply(function () {
                                    city.countyList = res.data
                                    checkSelect()
                                })
                            } else {
                                $rootScope.alertModal({"content":"系统异常"});
                            }
                        })
                    }
                    city.open = city.open ? !city.open : true
                }

                // 勾选操作
                $scope.addPlace2 = function () {
                    console.log($scope.selectedPlace)

                    var len = arguments.length;
                    for (var i = 0; i < $scope.selectedPlace.length; i++) {
                        if ($scope.selectedPlace[i].id == arguments[0].id) {
                            $scope.selectedPlace.splice(i, 1)
                            $timeout(function () {
                                checkSelect();
                            }, 0)
                            return false
                        }
                    }
                    $scope.selectedPlace.push({ name: arguments[0].name, id: arguments[0].id })
                    $timeout(function () {
                        checkSelect();
                    }, 0)

                }

                $scope.addPlace = function () {
                    console.log($scope.selectedPlace)
                    var len = arguments.length;
                    for (var i = 0; i < $scope.selectedPlace.length; i++) {
                        var idArr = $scope.selectedPlace[i].id.split(',')
                        // console.log(idArr)
                        if (idArr[idArr.length - 1] == arguments[len - 2].id) {
                            $scope.selectedPlace.splice(i, 1)
                            return false
                        }
                    }
                    if (len == 2) {
                        $scope.selectedPlace.push({ name: arguments[0].text, id: arguments[0].id })
                    } else if (len == 3) {
                        $scope.selectedPlace.push({
                            name: arguments[0].text + '/' + arguments[1].text,
                            id: arguments[0].id + ',' + arguments[1].id
                        })
                    } else if (len == 4) {
                        $scope.selectedPlace.push({
                            name: arguments[0].text + '/' + arguments[1].text + '/' + arguments[2].text,
                            id: arguments[0].id + ',' + arguments[1].id + ',' + arguments[2].id
                        })
                    }
                }

                // 勾选回显
                function checkSelect() {
                    function isEqual(o) {
                        if ($scope.selectedPlace.length == 0) {
                            return false
                        }
                        var idList = $scope.selectedPlace.reduce(function (pre, cur) {
                            var idArr = cur.id.split(',')
                            pre.push(idArr[idArr.length - 1])
                            return pre
                        }, [])
                        // console.log(o)
                        return idList.some(function (t) {
                            return o.id == t
                        })
                    }

                    // setTimeout(function () {
                    // $scope.$apply(function () {
                    // console.log(Object.prototype.toString.call($scope.provinces))
                    $scope.provinces.forEach(function (t) {
                        t.checked = isEqual(t)
                        if (t.cityList && t.cityList.length) {
                            t.cityList.forEach(function (m) {
                                m.checked = isEqual(m)
                                if (m.countyList && m.countyList.length) {
                                    m.countyList.forEach(function (n) {
                                        n.checked = isEqual(n)
                                    })
                                }
                            })
                        }
                    })
                    // })
                    // }, 0)
                    if ($scope.matchResults) {
                        $scope.matchResults.forEach(function (t) {
                            var idArr = t.id.split(',')
                            t.checked = isEqual({ id: idArr[idArr.length - 1] })
                        })
                    }
                }

                $scope.deleteAll = function () {
                    $scope.selectedPlace = [];
                    checkSelect();
                }
                $scope.remove = function (index) {
                    $scope.selectedPlace.splice(index, 1)
                    checkSelect()
                }

                $scope.submit = function (v) {
                    if (v == '1') {
                        $scope.selectedPlace = origionPlace
                    } else if (v == '2') {

                    }
                    $scope.showAdminiDivision = false
                    if ($scope.selectedPlace.length == 0) {  // 已选区域为零默认选择不限
                        $scope.setPlace = '0'
                    }
                }
                //搜索城市
                $scope.search = function () {
                    // alert(123)
                    if (!$scope.targetPlace) return
                    $.ajax({
                        url : $rootScope.app.project_name+'/zzyLaunchPlan/searchCity',
                        data: {'cityName':  $scope.targetPlace}
                    }).then(function (res) {
                        console.log(res)
                        if (res.httpCode == 200) {
                            $scope.$apply(function () {
                                // console.log(res.records.length)
                                if (!res.data.length) {
                                    $scope.dropShow = false //搜索数据为空，隐藏下拉框
                                    return // 搜索城市不存在，查询到的数组为空     
                                }
                                $scope.matchResults = res.data
                                $scope.dropShow = true
                                checkSelect()
                            })
                        } else {
                            $rootScope.alertModal({"content":"系统异常"});
                        }
                    })
                }

                //点击界面隐藏下拉框
                $scope.containHidden = function (e) {
                    $scope.dropShow = false
                }

                function stopPropagation(e) {
                    if (e.stopPropagation)
                        e.stopPropagation();
                    else
                        e.cancelBubble = true;
                }
                //点击自身下拉框不隐藏
                $scope.containShow = function (e) {
                    stopPropagation(e)
                }
            }]
        }
    })
    .controller('setStrateController', ['$rootScope', '$scope', '$http', '$state', function ($rootScope, $scope, $http, $state) {
        console.log($state.params);
        $scope.setPlace = '0'
        $scope.planName = $state.params.planName;
        // 保存数据
        $scope.save = function () {
            $.ajax({
                url : $scope.app.project_name+'/zzyLaunchPlan/addTactics',
                data: {
                    cityName:  $scope.targetPlace,
                    accountType: '1',
                    targetLocation1: $scope.selectedPlace
                }
            }).then(function (res) {
                if (res.httpCode == 200) {
                    $scope.$apply(function () {
                        console.log(res)
                    })
                } else {
                    $scope.alertModal({"content":"系统异常"});
                }
            })
        }
        //返回上一个路由
        $scope.goBack = function () {
            $state.go("main.biz.launchplan.planDetail",{adPlanId:$state.params.adPlanId})
        }
        //回显数据
        $.ajax({
            url : $scope.app.project_name+'/zzyLaunchPlan/queryTactics',
            data: {
                planId: $state.params.adPlanId,
                accountType: '1'
            }
        }).then(function (res) {
            console.log(res);
            if (res.httpCode == 200) {
                $scope.$apply(function () {
                    $scope.selectedPlace = res.data && res.data.targetLocation1 ? JSON.parse(res.data.targetLocation1) : [];
                    if ($scope.selectedPlace.length) {
                        $scope.setPlace = '1'

                    } else {
                        $scope.setPlace = '0'
                    }
                })
                console.log($scope.selectedPlace)
            } else {
                $scope.alertModal({"content":"系统异常"});
            }
        })
    }])